package org.intellij.doctree.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.util.ui.Tree;
import org.intellij.doctree.DocTree;
import org.intellij.doctree.DocTreeTransferHandler;
import org.intellij.doctree.node.DocTreeNode;
import org.intellij.doctree.node.FolderNode;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;

public class PasteNode extends TreeEditAction {

    public void actionPerformed(AnActionEvent e) {
        Tree tree = getTree(e);
        DocTreeTransferHandler th = (DocTreeTransferHandler) tree.getTransferHandler();
        Clipboard clipboard = th.getClipboard(tree);
        Transferable trans = clipboard.getContents(null);
        if (trans != null) {
            th.importData(tree, trans);
        }
    }

    public void update(AnActionEvent e) {
        boolean actionEnabled = false;
        DocTree tree = getTree(e);
        if (tree != null) {
            DocTreeNode node = tree.getCurrentNode();
            if (node != null && node instanceof FolderNode) actionEnabled = true;
        }
        e.getPresentation().setEnabled(actionEnabled);
    }

}
